package survey;

import gui.CameraPanel;
import se.lth.cs.realtime.JThread;
import util.Picture;

/**
 * The thread gets a picture from SurveyController and displays it at the gui
 */
public class PictureDisplayer extends JThread {
	private CameraPanel gui;
	private Synchronizer sc;
	private boolean run = true;

	/**
	 * Constructs the class
	 * 
	 * @param cameraPanel
	 *            gui in which the pictures is displayed.
	 * @param sc
	 *            where the pictures can be collected from
	 */
	public PictureDisplayer(CameraPanel cameraPanel, Synchronizer sc) {
		this.gui = cameraPanel;
		this.sc = sc;

	}

	/**
	 * Called when the thread is started.
	 */
	@Override
	public void run() {
		while (run) {
			Picture p = null;
			try {
				p = sc.getPicture();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			if(p != null) {
				gui.displayPicture(p);
			}
		}

	}
	
	public void kill() {
		this.run = false;
		sc.turnOff();
		try {
			this.join();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
